import Vue from 'vue'

import VueRouter from 'vue-router'
import PageHome from './components/Home.vue'
import DeviceSearch from './components/Device.vue'
import DeviceDefect from './components/Defect.vue'
import AccountSetting from './components/Setting.vue'
import DevicePreserve from './components/Preserve.vue'
import WorkOrder from './components/WorkOrder.vue'
import PreserveDetail from "./components/PreserveDetail.vue"
import DefectDetail from "./components/DefectDetail.vue"
import LoginPage from "./components/Login.vue"
import PageIndex from './components/PageIndex.vue'
import OrderDetail from './components/OrderDetail.vue'
import DeviceDetail from './components/DeviceDetail.vue'
import AdminSetting from './components/Setting.vue'
Vue.use(VueRouter)

const routes = [

    {
        path: '/index',
        name: 'PageIndex',
        component: PageIndex,
        meta: { requiresAuth: true },
        children: [{
            path: 'home',
            name: 'PageHome',
            component: PageHome
        }, {
            path: "preservedetail/:id", // 这里的id指代具体的项目id
            name: "PreserveDetail",
            component: PreserveDetail,
            props: true
        }, {
            path: "devicedetail/:id", // 这里的id指代具体的项目id
            name: "DeviceDetail",
            component: DeviceDetail,
            props: true
        },
        {
            path: "defectdetail/:id", // 这里的id指代具体的项目id
            name: "DefectDetail",
            component: DefectDetail,
            props: true
        },
        {
            path: "orderdetail/:id", // 这里的id指代具体的项目id
            name: "OrderDetail",
            component: OrderDetail,
            props: true
        },
        {
            path: 'device',
            name: 'DeviceSearch',
            component: DeviceSearch
        },
        {
            path: 'defect',
            name: 'DeviceDefect',
            component: DeviceDefect
        },
        {
            path: 'setting',
            name: 'AccountSetting',
            component: AccountSetting
        },
        {
            path: 'preserve',
            name: 'DevicePreserve',
            component: DevicePreserve
        },
        {
            path: 'setting',
            name: 'AdminSetting',
            component: AdminSetting
        },
        {
            path: 'workorder',
            name: 'WorkOrder',
            component: WorkOrder
        }]
    },
    {
        path: '/login',
        name: 'LoginPage',
        component: LoginPage
    },

]

const router = new VueRouter(

    {
        mode: 'history',
        routes
    })

router.beforeEach((to, from, next) => {
    // 检查要前往的路由是否需要登录
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 检查用户是否已登录
        if (!isLoggedIn()) {
            // 如果用户未登录, 重定向到登录界面
            next({
                path: '/login',
                query: { redirect: to.fullPath } // 把要跳转的地址作为参数，方便登录后直接跳转到该地址
            })
        } else {
            // 如果用户已登录，允许访问
            next()
        }
    } else {
        // 如果前往的路由没有设置需要登录，直接访问
        next()
    }
})
export default router
function isLoggedIn() {
    // 检查localStorage是否有用户信息
    if (localStorage.getItem('user')) {
      return true; //已登录
    } 
    return false; //未登录
  }
  